package com.ckh.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckh.springboot.common.Result;
import com.ckh.springboot.entity.HomeWorkStudent;
import com.ckh.springboot.entity.HomeWorkTeacher;
import com.ckh.springboot.entity.User;
import com.ckh.springboot.mapper.HomeWorkStudentMapper;
import com.ckh.springboot.mapper.HomeWorkTeacherMapper;
import com.ckh.springboot.mapper.UserMapper;
import com.ckh.springboot.service.IHomeWorkTeacherService;
import com.ckh.springboot.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/homeWorkTeacher")
@CrossOrigin(origins = "http://localhost:9528")
public class HomeWorkTeacherController {
    @Resource
    private IHomeWorkTeacherService iHomeWorkTeacherService ;
    @Resource
    private IUserService userService ;
    @Resource
    private UserMapper userMapper;
    @Resource
    private HomeWorkTeacherMapper homeWorkTeacherMapper;
    @Resource
    private HomeWorkStudentMapper homeWorkStudentMapper ;

    /**
     * 根据老师id 获取 老师发布的作业
     * @param id
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam("id") Integer id) {
        List<HomeWorkTeacher> list = iHomeWorkTeacherService.getHomeWorkTeacherList(id);
        return Result.success(list);
    }
    /**
     * 根据课程id和老师id发布作业
     */
    @GetMapping("/add")
    public Result add(@Param("classId") Integer classId ,
                      @Param("teachId") Integer teachId ,@Param("title") String title ,
                      @Param("text") String text , @Param("banji") String banji) {
        System.out.printf("z", classId);
        int i = iHomeWorkTeacherService.addHomeWorkTeacher(classId, teachId ,title , text , banji);
        /**
         * 获取学生列表
         * 遍历sys_user学生 得到每个学生的 identity = 0  student_id
         * homeworkid 查询sys_homework_teacher 里 id 最大的
         * insert into sys_homework_student 让所有学生  的submitcount = 0 student_id homeworkid
         *
         * 给所有学生 student_id  text homeworkid  的submitcount = 0
         *
         * 根据id 查 studentID
         * 根据studentId 设置 submitcount = 0
         */
        QueryWrapper<User>queryWrapper = new QueryWrapper<>() ;
        QueryWrapper<User> identity = queryWrapper.eq("identity", 0);
        List<User> users = userMapper.selectList(identity);
        System.out.println(users);

        QueryWrapper<HomeWorkTeacher> wrapper = new QueryWrapper<>();
        wrapper.select("max(id) as id");
        HomeWorkTeacher homeWorkTeacher = homeWorkTeacherMapper.selectOne(wrapper);
        System.out.println("maxId=" + homeWorkTeacher.getId());

        for(int j =0 ; j <users.size(); j ++ ) {
            User user = users.get(j);
            Integer studentId = Integer.valueOf(user.getStudentId());
            HomeWorkStudent homeWorkStudent = new HomeWorkStudent();
            homeWorkStudent.setStudentId(studentId);
            homeWorkStudent.setSubmitcount(0);
            homeWorkStudent.setHomeworkid(homeWorkTeacher.getId());
            homeWorkStudent.setText(text);
            homeWorkStudent.setName(user.getName());
            homeWorkStudentMapper.insert(homeWorkStudent);
        }
        if (i > 0 ) return Result.success();
        return Result.error();
    }
    /**
     * 根据班级获取作业
     */
    @GetMapping("/getClassHomeWork")
    public List<HomeWorkTeacher> getClassHomeWork(@Param("id") Integer id) {
        return iHomeWorkTeacherService.getClassHomeWork(id);
    }
}
